package com.hele.sts.action.admin;

import com.hele.sts.dao.*;
import com.hele.sts.pub.Constants;
import com.hele.sts.webwork.PageAction;
import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @ClassName DocMoveOutAction
 * @Description TODO
 * @Author xuning
 * @Date 2019-03-31 15:02
 **/
public class DocMoveOutAction extends PageAction {

    static Logger logger = LoggerFactory.getLogger(DocMoveOutAction.class);

    public static final String ACTION_MOVEOUT_LIST = "list";
    public static final String ACTION_TO_ADD_MOVEOUT = "to_add";
    public static final String ACTION_TO_EDIT_MOVEOUT = "to_edit";
    public static final String ACTION_ADD_MOVEOUT = "add";
    public static final String ACTION_EDIT_MOVEOUT = "edit";
    public static final String ACTION_DEL_MOVEOUT = "delete";
    public static final String ACTION_VIEW_MOVEOUT = "to_view";

    private String qQymc;
    private String qUniscid;
    private String qStartDate;
    private String qEndDate;

    private String id;

    private String action;

    private String errorInfo;

    private List<DocMoveOut> docMoveOutList;

    private DocMoveOut docMoveOut;

    private String selectedNbxh;

    private List<Bm> qyyyList;
    private List<Bm> djjgList;

    @Autowired
    private XtLogDao xtLogDao;

    @Autowired
    private DocMoveOutDao docMoveOutDao;

    @Autowired
    private BmDao bmDao;

    @Override
    public String execute() throws Exception {
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        String loginName = (String)session.get("AdminUser.loginName");
        if ((loginName == null) || (loginName.length() < 1))
        {
            setErrorInfo("没有登录");
            return "error";
        }
        if ((this.action == null) || (ACTION_MOVEOUT_LIST.equalsIgnoreCase(this.action))) {
            return super.execute();
        }
        if(ACTION_TO_ADD_MOVEOUT.equals(this.action)){
            //获取迁移原因列表
            qyyyList = bmDao.getBmList(BmDao.TABLE_QYYY);
            djjgList = DjjgDao.getDjjgList();

            //判断是否有已迁出未迁入的企业
            docMoveOut = docMoveOutDao.queryByNbxh(selectedNbxh);
            if(docMoveOut!=null&& docMoveOut.getStatus()==Constants.DocMoveStatus.MOVED_OUT){
                action = ACTION_TO_EDIT_MOVEOUT;
                return "to_add";
            }

            Qyhznr qyhznr = QyhznrDao.load(selectedNbxh);
            if(qyhznr!=null){
                docMoveOut = new DocMoveOut();
                docMoveOut.setNbxh(selectedNbxh);
                docMoveOut.setQymcOut(qyhznr.getQymc());
                docMoveOut.setUniscid(qyhznr.getUniscid());
                docMoveOut.setDjjg(qyhznr.getDjjg());
                Qyajb qyajb = QyajbDao.load(selectedNbxh);
                if(qyajb!=null){
                    docMoveOut.setAjh(qyajb.getAjh());
                }
            }else{
                logger.debug("需重新选择企业");
            }

            return "to_add";
        }
        if(ACTION_TO_EDIT_MOVEOUT.equals(this.action)){
            //获取迁移原因列表
            qyyyList = bmDao.getBmList(BmDao.TABLE_QYYY);
            djjgList = DjjgDao.getDjjgList();

            //判断是否有已迁出未迁入的企业
            docMoveOut = docMoveOutDao.queryById(id);
            if(docMoveOut!=null&& docMoveOut.getStatus()==Constants.DocMoveStatus.MOVED_OUT){
                return "to_add";
            }

            Qyhznr qyhznr = QyhznrDao.load(selectedNbxh);
            if(qyhznr!=null){
                docMoveOut = new DocMoveOut();
                docMoveOut.setNbxh(selectedNbxh);
                docMoveOut.setQymcOut(qyhznr.getQymc());
                docMoveOut.setUniscid(qyhznr.getUniscid());
                docMoveOut.setDjjg(qyhznr.getDjjg());
                Qyajb qyajb = QyajbDao.load(selectedNbxh);
                if(qyajb!=null){
                    docMoveOut.setAjh(qyajb.getAjh());
                }
            }else{
                logger.debug("需重新选择企业");
            }

            return "to_add";
        }
        if(ACTION_ADD_MOVEOUT.equals(this.action)){
            if(StringUtils.isBlank(docMoveOut.getId())){
                docMoveOut.setStatus(Constants.DocMoveStatus.MOVED_OUT);
                docMoveOutDao.addMoveOutDa(docMoveOut);
            }else {
                docMoveOutDao.updateMoveOutDa(docMoveOut);
            }
            errorInfo = "";
            return "ajax";
        }
//        if(ACTION_EDIT_MOVEOUT.equals(this.action)){
//            return "edit_moveout";
//        }
        if(ACTION_DEL_MOVEOUT.equals(this.action)){
            int res = docMoveOutDao.deleteMoveOutDa(id);
            errorInfo = "";
            return "ajax";
        }
        if(ACTION_VIEW_MOVEOUT.equals(this.action)){
            docMoveOut = docMoveOutDao.queryById(id);
            qyyyList = bmDao.getBmList(BmDao.TABLE_QYYY);
            for(Bm qyyy: qyyyList){
                if(qyyy.getCode().equals(docMoveOut.getReason())){
                    docMoveOut.setReason(qyyy.getContent());
                    break;
                }
            }
            djjgList = DjjgDao.getDjjgList();
            for(Bm bm: djjgList){
                if(bm.getCode().equals(docMoveOut.getTargetDjjg())){
                    docMoveOut.setTargetDjjg(bm.getContent());
                    break;
                }
            }
            return "to_view";
        }

        return "";
    }


    @Override
    public String getHref() {
        return null;
    }

    @Override
    public int count() {
        return docMoveOutDao.queryMoveOutCount(qQymc,qUniscid,qStartDate,qEndDate);
    }

    @Override
    public String list() {
        docMoveOutList = docMoveOutDao.queryMoveOutList(qQymc,qUniscid,qStartDate,qEndDate);
        return "list";
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getErrorInfo() {
        return errorInfo;
    }

    public void setErrorInfo(String errorInfo) {
        this.errorInfo = errorInfo;
    }

    public String getQQymc() {
        return qQymc;
    }

    public void setQQymc(String qQymc) {
        this.qQymc = qQymc;
    }

    public String getQUniscid() {
        return qUniscid;
    }

    public void setQUniscid(String qUniscid) {
        this.qUniscid = qUniscid;
    }

    public String getQStartDate() {
        return qStartDate;
    }

    public void setQStartDate(String qStartDate) {
        this.qStartDate = qStartDate;
    }

    public String getQEndDate() {
        return qEndDate;
    }

    public void setQEndDate(String qEndDate) {
        this.qEndDate = qEndDate;
    }

    public List<DocMoveOut> getDocMoveOutList() {
        return docMoveOutList;
    }

    public void setDocMoveOutList(List<DocMoveOut> docMoveOutList) {
        this.docMoveOutList = docMoveOutList;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public DocMoveOut getDocMoveOut() {
        return docMoveOut;
    }

    public void setDocMoveOut(DocMoveOut docMoveOut) {
        this.docMoveOut = docMoveOut;
    }

    public String getSelectedNbxh() {
        return selectedNbxh;
    }

    public void setSelectedNbxh(String selectedNbxh) {
        this.selectedNbxh = selectedNbxh;
    }

    public List<Bm> getQyyyList() {
        return qyyyList;
    }

    public void setQyyyList(List<Bm> qyyyList) {
        this.qyyyList = qyyyList;
    }

    public List getDjjgList() {
        return djjgList;
    }

    public void setDjjgList(List djjgList) {
        this.djjgList = djjgList;
    }
}
